package com.czapl.drawing;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.actions.RemoveActorAction;
import com.badlogic.gdx.utils.Array;
import com.czapl.api.IProjectile;
import com.czapl.api.IUnit;
import com.czapl.mygdxgame.MainScreen;

public abstract class CProjectile extends Actor implements IProjectile {

	protected Sprite m_sprite;
	protected float m_impactRadius;
	protected float m_speed;
	protected float m_baseDamage;
	private float m_size;
	private Vector2 m_position = new Vector2();

	public CProjectile(Vector2 launchCoordinates) {
		m_size = MainScreen.getGameBoard().getTileSize();
		setSize(m_size, m_size);
		MainScreen.getGameBoard().addActor(this);
		m_position.x = launchCoordinates.x + getWidth()/2;
		m_position.y = launchCoordinates.y + getHeight()/2;
		setX(launchCoordinates.x);
		setY(launchCoordinates.y);		
	}
	
	@Override
	public void draw (SpriteBatch batch, float parentAlpha) {
		Color color = getColor();
        batch.setProjectionMatrix(getStage().getCamera().combined);
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
    	batch.draw(m_sprite, getX(), getY(), getOriginX()+getWidth()/2, getOriginY()+getHeight()/2,
            getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
	}
	
	@Override
	public void act(float delta) {
		super.act(delta);
		m_position.x = getX()+getWidth()/2;
		m_position.y = getY()+getHeight()/2;;
	}
	
	@Override
	public void launch(IUnit unit) {
		Actor unitActor = (Actor) unit;
		float yDistance = unit.getPosition().y-m_position.y;
		float xDistance = unit.getPosition().x-m_position.x;
		setRotation((float) (Math.atan2(yDistance,xDistance)*180/Math.PI));
		
		MoveToAction moveToAction = new MoveToAction();
		moveToAction.setPosition(unitActor.getX(), unitActor.getY());
		float distance = (float) Math.sqrt( xDistance*xDistance +
				yDistance*yDistance );
		//System.out.println("(x1,y1): ("+unitActor.getX()+","+unitActor.getY()+") (x2,y2): ("+getX()+","+getY()+ ") distance: "+distance);
		moveToAction.setDuration(distance/m_speed);
		RemoveActorAction removeActorAction = new RemoveActorAction();
		ProjectileExplosionAction explosionAction = new ProjectileExplosionAction();
    	addAction(Actions.sequence(moveToAction, explosionAction, removeActorAction));
	}
	
	private class ProjectileExplosionAction extends Action {

		@Override
		public boolean act(float delta) {
			Array<IUnit> units = MainScreen.getGameBoard().getUnits();
			for(IUnit unit : units) {
				if(isUnitInRange(unit)) {
					unit.damage(m_baseDamage);
				}
			}
			System.out.println("explode");
			return true;
		}
		
		public boolean isUnitInRange(IUnit unit) {
			Vector2 unitPosition = unit.getPosition();
			//float unitRadius = unit.getRadius();
			float distance = (float) Math.sqrt( (unitPosition.x-m_position.x)*(unitPosition.x-m_position.x)+
					(unitPosition.y-m_position.y)*(unitPosition.y-m_position.y));
			return distance < unit.getRadius()+m_impactRadius;
			//circle equation
//			return (unitPosition.x - m_position.x)*(unitPosition.x - m_position.x) + 
//					(unitPosition.y - m_position.y)*(unitPosition.y - m_position.y) < m_impactRadius*m_impactRadius;
		}
	}
}
